import {
  changeModal,
  changeParams,
  getAdminUserAsync,
} from "@/store/adminUser/adminUserSlice";
import { Card, Space, Button } from "antd";
import { isEqual } from "lodash-es";
import { useAppDispatch, useAppSelector } from "@/store/hooks";

export default function Filter() {
  const dispatch = useAppDispatch();

  const { params, table } = useAppSelector((state) => state.adminUser);

  return (
    <Card>
      <Space
        wrap
        size="large"
        className="w-full flex justify-between"
        classNames={{ item: "last:ml-auto" }}
      >
        <Button type="primary" ghost onClick={():void => {
          dispatch(changeModal({type: 'create', open: true}))
        }}>
          创建
        </Button>
        <Button
          loading={table.status === "loading"}
          type="primary"
          onClick={(): void => {
            if (isEqual(params, table?.params)) {
              dispatch(getAdminUserAsync({...params}));
            } else {
              const newParams = {
                ...params,
                page: 1,
              };
              dispatch(changeParams(newParams));
              dispatch(getAdminUserAsync(newParams));
            }
          }}
        >
          查询
        </Button>
      </Space>
    </Card>
  );
}
